package com.fqgj.xjd.user.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.dao.UserDao;
import com.fqgj.xjd.user.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: zhangxingyun
 * Date: 2017-07-20
 * Time: 18:38:52.676
 */

@Repository
public class UserDaoImpl extends AbstractBaseMapper<UserEntity> implements UserDao {

    @Override
    public UserEntity selectByPrimaryKey(long id) {
        return getSqlSession().selectOne(getStatement("selectByPrimaryKey"), id);
    }

    /**
     * @param userCode
     * @return
     */
    @Override
    public UserEntity selectByUserCode(String userCode) {
        return getSqlSession().selectOne(getStatement("selectByUserCode"), userCode);
    }

    @Override
    public List<UserEntity> selectByMobile(String mobile) {
        return getSqlSession().selectList(getStatement("selectByMobile"), mobile);
    }

    @Override
    public List<UserEntity> selectByMD5Mobile(String md5Mobile) {
        return getSqlSession().selectList(getStatement("selectByMD5Mobile"), md5Mobile);
    }

    @Override
    public List<UserEntity> selectByMD5MobileIdNo(String md5MobileIdNo) {
        return getSqlSession().selectList(getStatement("selectByMD5MobileIdNo"), md5MobileIdNo);
    }

    @Override
    public List<UserEntity> selectByMobileAndCategories(String mobile, List<String> categories) {
        Map params = new HashMap();
        params.put("mobile", mobile);
        params.put("categories", categories);
        return getSqlSession().selectList(getStatement("selectByMobileAndCategories"), params);
    }

    @Override
    public List<Long> selectByName(String name) {
        List<Long> userIdList = new ArrayList<>();
        List<UserEntity> userEntityList = getSqlSession().selectList(getStatement("selectByName"), name);
        if (CollectionUtils.isNotEmpty(userEntityList)) {
            for (UserEntity userEntity : userEntityList) {
                userIdList.add(userEntity.getId());
            }
        }
        return userIdList;
    }

    @Override
    public int countByMobile(String mobile) {
        return getSqlSession().selectOne(getStatement("countByMobile"), mobile);
    }

    @Override
    public List<UserEntity> selectByUserCodes(List<String> userCodes) {
        if (CollectionUtils.isEmpty(userCodes)) {
            return null;
        }

        return getSqlSession().selectList(getStatement("selectByUserCodes"), userCodes);
    }

    @Override
    public List<UserEntity> selectByMobileAndPage(String mobile, Page page) {
        if (StringUtils.isEmpty(mobile)) {
            return null;
        }
        if (page != null) {
            if (page.isFirstPage()) {
                page.setTotalCount(this.countByMobile(mobile));
            }
        }

        Map params = new HashMap();
        params.put("mobile", mobile);
        params.put("page", page);
        return getSqlSession().selectList(getStatement("selectByMobileAndPage"), params);
    }

    @Override
    public Boolean updateUserName(Long userId, String userName) {
        UserEntity userEntity = this.selectByPrimaryKey(userId);
        if (userEntity == null) {
            return false;
        }

        userEntity.setName(userName);
        return this.updateByPrimaryKey(userEntity) > 0;
    }

    @Override
    public UserEntity selectByIdentityNo(String identityNo) {
        return getSqlSession().selectOne(getStatement("selectByIdentityNo"), identityNo);
    }

    /**
     * 根据身份证获取用户列表
     *
     * @param identityNo
     */
    @Override
    public List<UserEntity> selectUserListByIdentityNo(String identityNo) {
        Map<String, Object> params = new HashMap();
        params.put("identityNo", identityNo);
        params.put("deleted", 0);
        return getSqlSession().selectList(getStatement("selectUserListByParams"), params);
    }


    @Override
    public UserEntity selectByIdentityNoAndAppCode(String identityNo, String appCode, Integer biz) {
        Map params = new HashMap();
        params.put("identityNo", identityNo);
        params.put("appCode", String.valueOf(appCode));
        params.put("biz", biz);
        return getSqlSession().selectOne(getStatement("selectByIdentityNoAndAppCode"), params);
    }

    @Override
    public List<UserEntity> selectByNamePrefix(String namePrefix) {
        return getSqlSession().selectList(getStatement("selectByNamePrefix"), namePrefix);
    }


    @Override
    public UserEntity selectByCustomerCode(String customerCode) {
        return getSqlSession().selectOne(getStatement("selectByCustomerCode"), customerCode);
    }

    @Override
    public List<UserEntity> selectUserListByIdentityNoAndUserCategories(String identityNo, List<String> categories) {
        Map<String, Object> params = new HashMap();
        params.put("identityNo", identityNo);
        params.put("deleted", 0);
        params.put("categories", CollectionUtils.isEmpty(categories) ? null : categories);
        return getSqlSession().selectList(getStatement("selectUserListByIdentityNoAndUserCategories"), params);
    }

    @Override
    public int initInsert(UserEntity entity) {
        return getSqlSession().insert(getStatement("initInsert"), entity);
    }
}